<?php
/**
 * Created by Zend Studio.
 * User: FDG348
 * Date: 2017/9/9
 * Time: 11:39
 */
namespace common\models;

use common\models\GoodsIdentification;
use yii\db\ActiveRecord;

class ContentArticle extends ActiveRecord{
    const STATUS_ENABLE = 1;
    const STATUS_DISABLE = 0;


	const USER_FLG_YES	= 1;	//启用
	const USER_FLG_NO	= 0;	//禁用
	const RELEASE_YES	= 1;	//发布
	const RELEASE_NO	= 0;	//未发布

	public  $page_num		= 10;		//文章的默认分页
	public  $page_num_ext	= 1;		//文章的默认分页扩展（分页的第几条开始；默认第二条开始）

    public static function tableName(){
        return '{{%content_article}}';
    }
    
    public function rules(){
        return [
            ['use_flg','default','value'=>self::STATUS_DISABLE],
            ['creator','getCreator']
        ];
    }
    
    /**
     * @获取创建人
     */
    public function getCreator(){
        $userInfo = \common\helps\tools::getloginUserInfo();
        if($userInfo){
            $userName = $userInfo->userName;
        }else{
            $userName = '';
        }
        return $userName;
    }

	/**
	 * @param 获取一条文章信息
	 */
	public static function getOne($id){
		return self::findOne($id);
	}

	/**
	 * @param add by 2018/04/17 584 【商城需求】文章列表页展示调整 start
	 * @param 关联文章标识
	 * @return $this
	 */
	/*public function getContentArticle()
	{
		return $this->hasOne(GoodsIdentification::className(), ['id'=>'article_identification']);
	}*/

	/**
	 * @param add by 2018/04/17 584 【商城需求】文章列表页展示调整 start
	 * @param int $categoryId		文章分类
	 * @param int $page				分页
	 * @return array|\yii\db\ActiveRecord[]
	 */
	public function getContentArticleList($categoryId=ARTICLE_CATEGORY_ARTICLE,$page=1){
		return self::find()->where([
			self::tableName().'.category_id'=>$categoryId,
			self::tableName().'.is_release'=>self::RELEASE_YES,		//发布
			self::tableName().'.use_flg'=>self::USER_FLG_YES,		//启用
		])
			->joinWith('articleIdentification')
			->orderBy([self::tableName().'.sort'=>SORT_DESC])
			->offset(($page-1)*$this->page_num+$this->page_num_ext)
			->limit($this->page_num)
			->asArray()
			->all();
			//->createCommand()->getRawSql();
			/*->asArray()
			->offset(0)
			->limit(1)
			->all();*/
	}


    /**
     * @return \yii\db\ActiveQuery获取打标扩展信息
     * @param 关联对应的标识
     */
    public function getArticleIdentification(){
        return $this->hasOne(GoodsIdentification::className(), ['id'=>'article_identification']);
    }

}